var TcRevisit = {
    id: "TcRevisitTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

function _cellStyles(value, row, index) {
    if (row.revisitStatus === 2) {
        return {css: {"background-color": "#fff1d5", "overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    } else {
        return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    }
}

TcRevisit.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {
            title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '患者姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' style='color: red;display: flex;align-items: center;' '><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '联系电话', field: 'customerPhone', align: 'center', valign: 'middle', width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '创建人', field: 'createName', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访人员', field: 'revisit', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '跟踪状态', field: 'followStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '消费状态', field: 'consumptionStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访内容', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 200,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访结果', field: 'revisitContent', align: 'center', valign: 'middle', width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访时间', field: 'revisitTime', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return value.substring(0, 11);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return value.substring(0, 11);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '完成时间', field: 'updateTime', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return value.substring(0, 11);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访状态', field: 'revisitStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return value === 1 ? '待计划' : value === 2 ? '回访完成' : value === 3 ? '回访未完成' : value === 4 ? '回访取消' : '';
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        }
    ];
    return columns;
};
//点击上面状态筛选表格
TcRevisit.today = function () {
    $("#startRevisitTime").val(setDateTime(new Date(), 0) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(new Date(), 0) + " " + "23:59:59");
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    TcRevisit.search();
};
//明天
TcRevisit.tomorrow = function () {
    $("#startRevisitTime").val(setDateTime(new Date(), -1) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(new Date(), -1) + " " + "23:59:59");
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    TcRevisit.search();
};
//本周
TcRevisit.thisWeek = function () {
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {
        stepSunDay = -7;
    }
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    $("#startRevisitTime").val(setDateTime(monday, 0) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(sunday, 0) + " " + "23:59:59");
    $(".timeBox > div button").eq(2).addClass('actionClick').siblings().removeClass('actionClick');
    TcRevisit.search();
};

//检查是否选中
TcRevisit.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TcRevisit.seItem = selected[0];
        return true;
    }
};
//查询预约客户列表
TcRevisit.search = function () {
    TcRevisit.table.refresh({query: TcRevisit.formParams()});
    // TcRevisit.total();
};
//人数统计
// TcRevisit.total = function () {
//     var reach = $("#marketInputPublic", parent.document).val();
//     var startRevisitTime = $("#startRevisitTime").val();  //开始回访时间
//     var endRevisitTime = $("#endRevisitTime").val();  //结束回访时间
//     var startTime = $("#startTime").val();
//     var endTime = $("#endTime").val();
//     var startUpdateTime = $("#startUpdateTime").val();
//     var endUpdateTime = $("#endUpdateTime").val();
//     var followStatus = $("#followStatus").val();
//     var consumptionStatus = $("#consumptionStatus").val();
//     var revisit = $("#revisit").val();  //回访人员
//     $.ajax({
//         type: "GET",
//         url: Feng.ctxPath + "/tcRevisit/total",
//         data: {
//             reach: reach,
//             startRevisitTime: startRevisitTime,
//             endRevisitTime: endRevisitTime,
//             startTime: startTime,
//             endTime: endTime,
//             startUpdateTime: startUpdateTime,
//             endUpdateTime: endUpdateTime,
//             followStatus: followStatus,
//             consumptionStatus: consumptionStatus,
//             revisit: revisit,
//         },
//         dataType: "json",
//         success: function (data) {
//             $("#totalNum").html(data[0].countNum);//总人数
//             $("#planned").html(data[1].countNum);//待计划
//             $("#finished").html(data[2].countNum);//已完成
//             $("#unfinished").html(data[3].countNum);//未完成
//             $("#tocancel").html(data[4].countNum);//取消
//         }
//     });
// };
//人数统计 + 表格刷新
TcRevisit.inquire = function () {
    TcRevisit.search();
};
//查询表单提交参数对象 @returns {{}}
TcRevisit.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#marketInputPublic", parent.document).val().trim();
    queryData['revisitStatus'] = $("#revisitStatus").val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['startUpdateTime'] = $("#startUpdateTime").val();
    queryData['endUpdateTime'] = $("#endUpdateTime").val();
    queryData['followStatus'] = $("#followStatus").val();
    queryData['consumptionStatus'] = $("#consumptionStatus").val();
    queryData['revisit'] = $("#revisit").val();
    queryData['createName'] = $("#createName").val();
    queryData['outpatientName'] = $("#outpatientName",parent.document).val();
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    return queryData;
};
//根据点击上面状态刷新表格
TcRevisit.totalAllNumber = function (obj) {
    $("#revisitStatus").val(obj);
    TcRevisit.inquire();
};
//重置
TcRevisit.resetSearch = function () {
    $("#doctorTcInputPublic", parent.document).val("");
    $("#startRevisitTime").val("");
    $("#endRevisitTime").val("");
    $("#startTime").val("");
    $("#endTime").val("");
    $("#startUpdateTime").val("");
    $("#endUpdateTime").val("");
    $("#consumptionStatus").val("");
    $("#followStatus").val("");
    $("#revisit").val("");
    $("#createName").val("");
    TcRevisit.inquire();
};
//初始化赋值时间 + 请求数据
TcRevisit.time = function () {
    $("#revisitTimeTwo").val(setDateTime(new Date(), 0));
};
//主页搜索 tc
$("#marketSearchAll", parent.document).click(function () {
    TcRevisit.inquire();
});

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#TcRevisitTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
$(function () {
    var defaultColunms = TcRevisit.initColumn();
    var table = new BSTable(TcRevisit.id, "/tcRevisit/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(TcRevisit.formParams());
    TcRevisit.table = table.init();
    checkDate = function () {TcRevisit.inquire();};
    TcRevisit.today();
    TcRevisit.time();
    TcRevisit.totalAllNumber();
    $("#totalAllNumber").addClass('actionClick').siblings().removeClass('actionClick');
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {TcRevisit.inquire();});
    $("#zoom").click(function () {
        if($("#zoom > span").hasClass('tabMark')){
            $("#zoom > span").removeClass('tabMark').html('条件展开');
            $("#zoom > img,.divFlex").toggle();
            $(document).ready( function (){ $( '#ConsultantRecordTable' ).bootstrapTable('resetView',{ height: $(window).height()-76 } );});
        }else {
            $("#zoom > span").addClass('tabMark').html('条件收起');
            $("#zoom > img,.divFlex").toggle();
            $(document).ready( function (){ $( '#ConsultantRecordTable' ).bootstrapTable('resetView',{ height: $(window).height()-141 } );});
        }
    });
    phonePrivileges();
});